

clear all
set more off
set matsize 11000


set seed 277491  // first run: 9509732   from random.org

local gtools "g"  // code uses gtools. if you don't have gtools add "local gtools """ to your else if


*---------------------------------David's folders-------------------
if "`c(username)'"=="da334" | "`c(username)'"=="David" | "`c(username)'"=="Atkin" | "`c(username)'"=="dga" | "`c(username)'"=="atkin" {
global dump "C:/Scratch"
global dropbox "C:/Work/Engel_GFT"
global stataloc "C:/Dropbox/Stata15/StataMP-64"
global codeloc "$dropbox/replication_files/do_files"
global output "$dropbox/replication_files/data/intermediate_data/conventional_price_indices"
}
if "`c(username)'"=="dga" {
global dump "G:/Scratch" 
}
*-------------------------------------------------------------------

local foldno "1"
local bs=0



global share_spec "rshare"   // this is the type of shares in that file that we use, only takes one at a time for now
global band_spec "g25"  // this is bandwidth (formed in engel_lpoly_loop_v1_bs.do)


foreach bs of numlist 0  {  // for bootstrap code
local zzz "15"  //
global Gi_spec "V1_DM_G108_`zzz'"   // This is the hh file we use, this calls "$dropbox/replication_files/data/intermediate_data/hh_shares/hh_shares_43_50_55${Gi_spec}_rural.dta" 


*This pulls the number of markets from the data do this
use "$dropbox/replication_files/data/intermediate_data/hh_shares/hh_shares_43_55${Gi_spec}_rural.dta"  , clear  
sum market_id
local nmarket=r(max)






keep ${share_spec}_*
local prodcount=c(k)






******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************

*Block 4: Figures

******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************


******************************************************************************
******************************************************************************
******************************************************************************







***Now national paasche and laspeyres changes
 

foreach share  in $share_spec { 
foreach DMI in $Gi_spec { 
foreach band in $band_spec   { 







use "$dropbox/replication_files/data/intermediate_data/Engel_curves/Engel_V`foldno'/Estimate_`band'_`share'_`DMI'_bs`bs'.dta", clear


*bring in exact estimates run in exact_correction_v1.do
rename ZE_5543_decile XZE_5543_decile
rename ZE_4355_decile XZE_4355_decile
merge 1:1 decile using "$dropbox/replication_files/data/intermediate_data/Engel_curves/Engel_V`foldno'exactSnrP1/Estimate_`band'_`share'_`DMI'_bs`bs'.dta", keepusing(ZE_5543_decile)
drop _merge
rename ZE_5543_decile ZE_5543_decile_exact
merge 1:1 decile using "$dropbox/replication_files/data/intermediate_data/Engel_curves/Engel_V`foldno'exactSnrP0/Estimate_`band'_`share'_`DMI'_bs`bs'.dta", keepusing(ZE_4355_decile)
drop _merge
rename ZE_4355_decile ZE_4355_decile_exact
rename XZE_5543_decile ZE_5543_decile
rename XZE_4355_decile ZE_4355_decile




merge 1:1 decile using "$dropbox/replication_files/data/intermediate_data/Engel_curves/Engel_V`foldno'/Estimate_short_CPI_`band'_`share'_`DMI'_bs_combo.dta"




if "`bs'"=="0" {

*now plot the figures




*Figure 2
twoway bar ZY_4355_decile  decile , ///
xlabel(10(10)90) ylabel(100(50)250) xtitle("Decile of Income Distribution") ytitle("Percentage Change in Nominal Outlays") ///
barwidth(8 8 8 8) fcolor(edkblue%50 erose%50 none none  ) lcolor(edkblue erose edkblue maroon) lwidth(none   none medium medium) graphregion(lcolor(white) fcolor(white)) ///
note("") title("",size(medsmall))   || ///
(rcap ZY_4355_decile_pctile975_v2 ZY_4355_decile_pctile025_v2  decile, lcolor(edkblue) ) , legend(off) saving("${dump}/Engel`foldno'/YPaascheLaspeyres_CI_ZY_`band'_`share'_`DMI'_short.gph", replace)
graph export "$dropbox/replication_files/output/Figure2.pdf", replace as(pdf)





foreach var of varlist ZL_4355_decile ZE_4355_decile* ZL_demo_4355_decile   { 

if regexm("`var'","demo")==1 | regexm("`var'","pluto")==1 {
local nvar "District-Level CPI"
local nvar2 "District-Level CPI"
local pname "Paasche"
local lname "Laspeyres"
local wpname "Paasche"
local wlname "Laspeyres"
local ffcolor "edkblue%70 maroon%70"
local llcolor "edkblue maroon"
local flipffcolor "maroon%70 edkblue%70"
local flipllcolor "maroon edkblue"
}
else if regexm("`var'","^ZE")==1 {
local nvar "No Price Correction"
local nvar2 "AFFG Price Index"
local wnvar "CV and EV (AFFG Price Index)"
local pname "P{superscript:1}"
local lname "P{superscript:0}"
local wpname "EV"
local wlname "CV"
local ffcolor "eltblue%50 erose%50"
local llcolor "eltblue erose"
local flipffcolor "erose%50 eltblue%50 "
local flipllcolor "erose eltblue"

}
else {
local nvar "District-Decile-Level CPI"
local nvar2 "District-Decile-Level CPI"
local wnvar "Real Income (District-Decile-Level CPI)"
local pname "Paasche"
local lname "Laspeyres"
local wpname "Paasche"
local wlname "Laspeyres"
local ffcolor "edkblue%70 maroon%70"
local llcolor "edkblue maroon"
local flipffcolor "maroon%70 edkblue%70"
local flipllcolor "maroon edkblue"
}



if regexm("`share'","share")!=1  {
local nvar "CLM Exact Correction"
local nvar2 "CLM Exact Correction"
}


local Pflip=regexr("`var'","^L","P")
local Pflip=regexr("`Pflip'","^ZL","ZP")
local Pflip=regexr("`Pflip'","^ZE_4355","ZE_5543")
local Pflip=regexr("`Pflip'","^ZE_4350","ZE_5043")





*used in Fig 3
if  "`var'"=="ZE_4355_decile" {
twoway bar `var' `Pflip'  decile , ///
xlabel(10(10)90) ylabel(100(20)180) xtitle("Decile of Income Distribution", size(medlarge)) ytitle("") ///
barwidth(8 8 8 8) fcolor(eltblue%50 erose%50 none none  ) lcolor(eltblue erose edkblue maroon) lwidth(none   none medium medium) graphregion(lcolor(white) fcolor(white)) ///
note("") title("`nvar'",size(vlarge)) ///
legend(col(2) label(1 "`lname'") label(2 "`pname'")  ) saving("${dump}/Engel`foldno'/ZPaascheLaspeyres_`var'_`band'_`share'_`DMI'_FO.gph", replace)
}
if  "`var'"=="ZE_4355_decile_f07" {
twoway bar `var' `Pflip'  decile , ///
xlabel(10(10)90) ylabel(100(20)180) xtitle("Decile of Income Distribution", size(medlarge)) ytitle("") ///
barwidth(8 8 8 8) fcolor(eltblue%50 erose%50 none none  ) lcolor(eltblue erose edkblue maroon) lwidth(none   none medium medium) graphregion(lcolor(white) fcolor(white)) ///
note("") title("First-Order Price Correction",size(vlarge)) ///
legend(col(2) label(1 "`lname'") label(2 "`pname'")  ) saving("${dump}/Engel`foldno'/ZPaascheLaspeyres_`var'_`band'_`share'_`DMI'_FO.gph", replace)
}
if  "`var'"=="ZE_4355_decile_exact" {
twoway bar `var' `Pflip'  decile , ///
xlabel(10(10)90) ylabel(100(20)180) xtitle("Decile of Income Distribution", size(medlarge)) ytitle("") ///
barwidth(8 8 8 8) fcolor(eltblue%50 erose%50 none none  ) lcolor(eltblue erose edkblue maroon) lwidth(none   none medium medium) graphregion(lcolor(white) fcolor(white)) ///
note("") title("Exact Price Correction",size(vlarge)) ///
legend(col(2) label(1 "`lname'") label(2 "`pname'")  ) saving("${dump}/Engel`foldno'/ZPaascheLaspeyres_`var'_`band'_`share'_`DMI'_FO.gph", replace)
}


*used in Fig 4 
if "`var'"=="ZL_demo_4355_decile" | "`var'"=="ZE_4355_decile"  | "`var'"=="ZL_4355_decile" {
twoway bar `var' `Pflip'  decile , ///
xlabel(10(10)90) ylabel(100(20)200) xtitle("Decile of Income Distribution", size(medlarge)) ytitle("") ///
  fcolor(`ffcolor' none none  ) lcolor( `llcolor' edkblue maroon) lwidth(none   none medium medium) graphregion(lcolor(white) fcolor(white)) ///
barwidth(8 8 8 8) note("") title("`nvar2'",size(vlarge)) ///
legend(col(2) order(1 2) label(1 "`lname'") label(2 "`pname'")  ) || ///
(rcap `var'_pctile975_v2 `var'_pctile025_v2  decile, lcolor(`llcolor') ) || ///
(rcap `Pflip'_pctile975_v2 `Pflip'_pctile025_v2  decile, lcolor(`flipllcolor')), saving("${dump}/Engel`foldno'/ZPaascheLaspeyres_CI_`var'_`band'_`share'_`DMI'.gph", replace)
*symxsize(*.5)  colgap(*.5)  keygap(*.7)

twoway bar `var' `Pflip'  decile , ///
xlabel(10(10)90) ylabel(100(20)180) xtitle("Decile of Income Distribution", size(medlarge)) ytitle("") ///
 fcolor(`ffcolor' none none  ) lcolor( `llcolor' edkblue maroon) lwidth(none   none medium medium) graphregion(lcolor(white) fcolor(white)) ///
barwidth(8 8 8 8) note("") title("`nvar2'",size(vlarge)) ///
legend(col(2) order(1 2) label(1 "`lname'") label(2 "`pname'")  ) || ///
(rcap `var'_pctile975_v2 `var'_pctile025_v2  decile, lcolor(`llcolor') ) || ///
(rcap `Pflip'_pctile975_v2 `Pflip'_pctile025_v2  decile, lcolor(`flipllcolor')), saving("${dump}/Engel`foldno'/ZPaascheLaspeyres_CI_`var'_`band'_`share'_`DMI'_short.gph", replace)
}


if "`var'"=="ZL_4355_decile" | "`var'"=="ZE_4355_decile" {

*used in Fig 5
twoway bar  WW`Pflip' WW`var' decile , ///
xlabel(10(10)90) ylabel(0(10)50) xtitle("Decile of Income Distribution", size(medlarge)) ytitle("") ///
fcolor(`flipffcolor' none none  ) lcolor( `flipllcolor' edkblue maroon) lwidth(none   none medium medium) graphregion(lcolor(white) fcolor(white)) ///
barwidth(8 8 8 8) note("") title("`wnvar'",size(vlarge)) ///
legend(col(2) order(2 1) label(1 "`wpname'") label(2 "`wlname'")  ) || ///
(rcap  WW`Pflip'_pctile975_v2 WW`Pflip'_pctile025_v2 decile, lcolor( `flipllcolor') ) || ///
(rcap WW`var'_pctile975_v2 WW`var'_pctile025_v2  decile, lcolor(`llcolor')), saving("${dump}/Engel`foldno'/WWZPaascheLaspeyres_CI_`var'_`band'_`share'_`DMI'_short.gph", replace)

}











}


cd $dump/Engel`foldno'/



*this is Figure 3
grc1leg "ZPaascheLaspeyres_ZE_4355_decile_`band'_`share'_`DMI'_FO.gph"  ///
"ZPaascheLaspeyres_ZE_4355_decile_f07_`band'_`share'_`DMI'_FO.gph"  ///
"ZPaascheLaspeyres_ZE_4355_decile_exact_`band'_`share'_`DMI'_FO.gph"  ///
, imargin(tiny) title("", size(medsmall)) graphregion(lcolor(white) fcolor(white) margin(tiny))  col(3) ///
l1title("Percentage Change in Price Index", size(medium)) ycommon
graph display, xsize(7) ysize(3.75)
graph export "$dropbox/replication_files/output/Figure3.pdf", replace as(pdf) 


*this is figure 4
graph combine ZPaascheLaspeyres_CI_ZE_4355_decile_`band'_`share'_`DMI'_short.gph ZPaascheLaspeyres_CI_ZL_demo_4355_decile_`band'_`share'_`DMI'_short.gph  ZPaascheLaspeyres_CI_ZL_4355_decile_`band'_`share'_`DMI'_short.gph , imargin(tiny) title("", size(medsmall)) graphregion(lcolor(white) fcolor(white) margin(tiny))  col(3) ///
l1title("Percentage Change in Price Index", size(medium)) ycommon
graph display, xsize(7) ysize(3.75)
graph export "$dropbox/replication_files/output/Figure4.pdf", replace as(pdf) 

*this is figure 5
graph combine WWZPaascheLaspeyres_CI_ZE_4355_decile_`band'_`share'_`DMI'_short.gph WWZPaascheLaspeyres_CI_ZL_4355_decile_`band'_`share'_`DMI'_short.gph , imargin(tiny) title("", size(medsmall)) graphregion(lcolor(white) fcolor(white) margin(tiny))  col(3) ///
l1title("Percentage Change in Welfare", size(medium)) ycommon
graph display, xsize(7) ysize(3.75)
graph export "$dropbox/replication_files/output/Figure5.pdf", replace as(pdf) 









}





}
}
}
**/


}
*this is the loop for bootstrap



exit, clear STATA



